package com.tensquare.common.auth.config;

import com.tensquare.common.auth.interceptor.AuthorityInterceptor;
import com.tensquare.common.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author: WangYaWei
 * @description:
 * @create: 2019-08-26 11:09
 **/
@Configuration
@ConditionalOnWebApplication
@ConditionalOnProperty(prefix="intercaptor",name = "authorityEnable", havingValue = "true")
public class AuthorityConfig implements WebMvcConfigurer {
//public class AuthorityConfig extends WebMvcConfigurationSupport {
// spring.factories 配置的自动扫描WebMvcConfigurationSupport实现类 不加载addInterceptors方法

    @Qualifier("commonJwtUtil")
    @Autowired
    private JwtUtil jwtUtil;

    @Bean
    public AuthorityInterceptor authInterceptor(){
        return new AuthorityInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/**/login");
    }
}
